﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace MES_SystemUI.Helpers
{
    public class EncryptionAndDecryption
    {
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        /// <returns></returns>
        public static string GetSQLStr
        {
            get
            {
                return Decrypt(ConfigurationManager.ConnectionStrings["EMS_System"].ConnectionString, SystemDataConfig.KeyStr, SystemDataConfig.IVStr);
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="KeyStr">16位密钥，注意⚠️必须16位</param>
        /// <param name="IVStr">16位初始化向量，注意：必须16位，否则返回空</param>
        /// <returns>密文</returns>
        public static string Encrypt(string plainText, string KeyStr, string IVStr)
        {
            if (plainText == "") return null;
            if (KeyStr.Length != 16 || IVStr.Length != 16) return null;
            byte[] Key = Encoding.UTF8.GetBytes(KeyStr); // 16字节密钥
            byte[] IV = Encoding.UTF8.GetBytes(IVStr);  // 16字节初始化向
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plainText);
                        }
                        return Convert.ToBase64String(msEncrypt.ToArray());
                    }
                }
            }
        }
        /// <summary>
        /// 解密，密钥与初始化向量，需要和加密时一样
        /// </summary>
        /// <param name="cipherText">密文</param>
        /// <param name="KeyStr">16位密钥，注意⚠️必须16位</param>
        /// <param name="IVStr">16位初始化向量，注意：必须16位，否则返回空</param>
        /// <returns>明文</returns>
        public static string Decrypt(string cipherText, string KeyStr, string IVStr)
        {
            if (cipherText == "") return null;
            if (KeyStr.Length != 16 || IVStr.Length != 16) return null;
            byte[] Key = Encoding.UTF8.GetBytes(KeyStr); // 16字节密钥
            byte[] IV = Encoding.UTF8.GetBytes(IVStr);  // 16字节初始化向量
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                byte[] bytes = Convert.FromBase64String(cipherText);

                using (MemoryStream msDecrypt = new MemoryStream(bytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            try
                            {
                                return srDecrypt.ReadToEnd();
                            }
                            catch
                            {
                                return null;
                            }
                        }
                    }
                }
            }
        }
    }
}
